package com.pet.review.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pet.api.client.OrderClient;
import com.pet.api.client.UserClient;
import com.pet.api.dto.UserReviewInfoDTO;
import com.pet.common.domain.Result;
import com.pet.common.domain.UserContext;
import com.pet.review.domain.dto.ReviewDTO;
import com.pet.review.domain.po.Review;
import com.pet.review.domain.vo.ReviewVO;
import com.pet.review.mapper.ReviewMapper;
import com.pet.review.service.IReviewService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品评论表 服务实现类
 * </p>
 *
 * @author Cookies
 * @since 2024-07-11
 */
@Service
@RequiredArgsConstructor
public class ReviewServiceImpl extends ServiceImpl<ReviewMapper, Review> implements IReviewService {
    private final OrderClient orderClient;
    private final UserClient userClient;

    @Override
    @GlobalTransactional
    public Result addReviewByUser(ReviewDTO reviewDTO) {
        Integer userId = UserContext.getUser();
        Long orderId = reviewDTO.getOrderId();
        // 获取商品id列表
        List<Integer> itemIds = orderClient.getDetailByOrder(orderId);
        List<Review> reviewList = new ArrayList<>(itemIds.size());
        // 收集评论信息
        for (Integer itemId : itemIds) {
            // 判断是否已评论过
            Integer count = lambdaQuery()
                    .eq(Review::getUserId, userId)
                    .eq(Review::getItemId, itemId)
                    .count();
            if (count > 0) {
                continue;
            }
            // 保存评论信息
            Review review = new Review();
            review.setUserId(userId);
            review.setItemId(itemId);
            review.setReviewText(reviewDTO.getReviewText());
            review.setRating(reviewDTO.getRating());
            reviewList.add(review);
        }
        boolean isSuccess = saveBatch(reviewList);
        // 修改订单状态为已评价, 结束订单流程
        orderClient.updateOrderByReview(orderId);
        return isSuccess ? Result.success() : Result.error();
    }

    @Override
    public Result getReviewListByItem(Integer itemId) {
        List<Review> reviewList = lambdaQuery()
                .eq(Review::getItemId, itemId)
                .list();
        if (reviewList == null || reviewList.isEmpty()) {
            return Result.success(Collections.emptyList());
        }
        Set<Integer> userIds = reviewList.stream()
                .map(Review::getUserId)
                .collect(Collectors.toSet());
        Map<Integer, UserReviewInfoDTO> userInfoMap = userClient.queryUserReviewInfo(userIds);
        List<ReviewVO> reviewVOList = new ArrayList<>(reviewList.size());
        reviewList.stream()
                .forEach(review -> {
                    ReviewVO reviewVO = BeanUtil.copyProperties(review, ReviewVO.class);
                    UserReviewInfoDTO userInfoDTO = userInfoMap.get(review.getUserId());
                    reviewVO.setNickname(userInfoDTO.getNickname());
                    reviewVO.setPhoto(userInfoDTO.getPhoto());
                    reviewVOList.add(reviewVO);
                });
        return Result.success(reviewVOList);
    }
}
